package com.wyq.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.mail.internet.InternetAddress;
import javax.servlet.ServletContext;

import org.apache.commons.mail.HtmlEmail;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wyq.bean.Audience;
import com.wyq.service.AudienceService;
import com.wyq.service.MailInfoService;
import com.wyq.service.ShowInfoService;
import com.wyq.util.PropertiesUtil;

/**
 * 后台管理-重发邮件
 * @author Fyj
 *
 */
public class ReSendEmailAction extends ActionSupport {
	private AudienceService audienceService;
	private ShowInfoService showInfoService;
	private MailInfoService mailInfoService;
	private String selIDStr;
	private String jsonStr;
	private String show;
	public void setAudienceService(AudienceService audienceService) {
		this.audienceService = audienceService;
	}

	public void setShowInfoService(ShowInfoService showInfoService) {
		this.showInfoService = showInfoService;
	}

	public void setMailInfoService(MailInfoService mailInfoService) {
		this.mailInfoService = mailInfoService;
	}

	public void setSelIDStr(String selIDStr) {
		this.selIDStr = selIDStr;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public String reSendEmailToAudience(){
		ServletContext context=ServletActionContext.getServletContext();
		
			Audience audience = new Audience();
			List showInfoList = new ArrayList();
			List mailInfoList = new ArrayList();
			int successFlag = 0;
			int errorFlag = 0;
			int selId = -1;
			String[] selIDArr = null;
			if(selIDStr != null){
				selIDArr = selIDStr.split(",");
			}
			for(int i=0;i<selIDArr.length;i++){
				if(selIDArr[i] != null){
					try {
					//循环发送邮件
					selId = Integer.parseInt(selIDArr[i]);
			
					audience = audienceService.findAudienceById(selId);
					showInfoList = showInfoService.getShowInfoById("1");
					
					mailInfoList = mailInfoService.getMailInfo();
					
					if(showInfoList.size() <= 0){
						return ERROR;
					}
					if(mailInfoList.size() <= 0){
						//未设置发送邮信息的邮箱
						return ERROR;
					}
					
					Map showInfo = (Map)showInfoList.get(0);
					//当前可用的服务邮箱有且只有一个
					Map mailInfo = (Map)mailInfoList.get(0);
					
					String fromMail = mailInfo.get("mailServerAddress").toString();
					String fromMailPassword = mailInfo.get("mailServerPassword").toString();
					String fromMailHost = "smtp." + (fromMail.split("@"))[1];
					
					/*Map request = (Map)ActionContext.getContext().get("request");
			        //注册成功后成卡号
			        audience.setId(9900000 + audience_id);
			        request.put("audience", audience);
			        request.put("showInfo", showInfo);*/
				
					//登记用户的邮箱
					String toMail = audience.getAudience_Email();
					System.out.println("toMail:::::::" + toMail);
					
					//发送邮件
					HtmlEmail email = new HtmlEmail();
					email.setHostName(fromMailHost);
					email.setAuthentication(fromMail, fromMailPassword);
					email.setFrom(fromMail);
					Vector<InternetAddress> emailV = new Vector<InternetAddress>();
					InternetAddress to = new InternetAddress(toMail);
					emailV.add(to);
					email.setTo(emailV);
					email.setCharset("UTF-8");
					email.setSubject("欢迎参加"+showInfo.get("showName")+"展会");
					System.out.println("path="+context.getRealPath("/"));
					File img = new File(context.getRealPath("/")+"/images/title.jpg");  
					
					
					StringBuffer buffer = new StringBuffer();  
				 
				 //	buffer.append("<center style='font-size: 10pt;'>");
					buffer.append("<table width='800px' align='center' cellpadding='0' cellspacing='0' style='font-size: 10pt;width:679px; margin:0 auto; background-color:#FFF'>");
					buffer.append("<tr><td width='800px' height='200px'><img src='cid:"+email.embed(img)+"' width='800' height='200'></td></tr>");
					buffer.append("<tr>");
					buffer.append("<td bgcolor='#FFFFFF' class='fontblack'>");
					buffer.append("<table cellpadding='2' cellspacing='2' style='border:1px solid #999999; padding:10px' width='800px'>");
					/////////
					buffer.append("<tr>");
					buffer.append("<td>");
					buffer.append("尊敬的" + audience.getAudience_name());
					String audience_sex = audience.getAudience_sex();
					if(audience_sex.equals("0")){
						buffer.append("先生");
					}else if(audience_sex.equals("1")){
						
						buffer.append("女士");
					}
					buffer.append("：您好");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr><td height='5px'></td></tr>");
					buffer.append("<tr>");
					buffer.append("<td style='padding-left: 20px'>感谢您注册参观<b>" + showInfo.get("showName").toString() + "</b>，您的预登记信息已经被正式确认！</td>");
					buffer.append("</tr>");
					buffer.append("<tr><td height='5px'></td></tr>");
					buffer.append("<tr>");
					buffer.append("<td style='padding-left: 20px'>");
					buffer.append("请仔细核对以下参观信息，并打印出此参观确认函(务必可以清晰地看到卡号)，至展会现场预登记观众接待处出示此参观确认函及参观人有效证件(身份证或两张名片均可)，快速换取观众胸卡进入参观。");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("</table>");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td height='10px'></td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td>");
					buffer.append("<table cellpadding='2' cellspacing='2' style='border:1px solid #999999; padding:10px' width='800px'>");
					buffer.append("<tr height='30px'>");
					buffer.append("<td align='center'>");
					buffer.append("<b style='font-size: 18px;'>" + showInfo.get("showName").toString() + "</b>");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td>");
					buffer.append("<b>专业参观时间：</b>" + showInfo.get("showTime").toString());
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td>");
					buffer.append("<b>展会地点：</b>" + showInfo.get("showAddress").toString());
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td height='5px'></td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td>");
					buffer.append("<b>姓名：</b>" + audience.getAudience_name());
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td>");
					buffer.append("<b>预登记号：</b>"+(9900000+audience.getId()));
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td>");
					buffer.append("<img src='"+PropertiesUtil.getVal("weburl")+"/audienceCode.action?code="+ (9900000+audience.getId()) +"&barType=CODE39&checkCharacter=n&checkCharacterInText=n' width='400px' height='100px'/>");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("</table>");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td height='10px'align='right'><a href='#' onclick='javascript:window.print();'>点击打印</a></td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td>");
					buffer.append("	<table cellpadding=2' cellspacing=2' style='border:1px solid #999999; padding:10px' width='800px'>");
					buffer.append("<tr>");
					buffer.append("<td>");
					buffer.append("<b>注意事项：</b>");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td style='padding-left: 20px'>");
					buffer.append("• 本邀请函仅限专业观众参观日期间使用（"+showInfo.get("showTime").toString()+"）。");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td style='padding-left: 20px'>");
					buffer.append("• 此确认函不可转让，出售，只可由本人使用一次。请务必带上此确认函及个人名片(两张)前往展会现场，否则，主办方有权利收取您30元的门票费用。");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td style='padding-left: 20px'>");
					buffer.append("• 此信息只能使用一次，第二个持此相同信息的人将无法使用。如有同公司多人登记，请分别独立注册。"); 
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td>");
					buffer.append("<b>如有疑问请联系我们：</b>");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td style='padding-left: 20px'>");
					buffer.append("电话："+showInfo.get("showTel").toString());
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td style='padding-left: 20px'>");
					buffer.append("传真："+showInfo.get("showFax").toString());
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td style='padding-left: 20px'>");
					buffer.append("Email："+showInfo.get("showEmail").toString());
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr>");
					buffer.append("<td style='padding-left: 20px'>");
					buffer.append("网址：" + showInfo.get("showCompany").toString());
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("<tr height='10px'>");
					buffer.append("	<td></td>");
					buffer.append("</tr>");
					buffer.append("</table>");
					buffer.append("</td>");
					buffer.append("</tr>");
					buffer.append("</table>");
		        
					email.setHtmlMsg(buffer.toString());
					email.send();
					
					successFlag++;
					} catch (Exception e) {
						e.printStackTrace();
						errorFlag++;
						continue;
						//jsonStr = "{success:false,data:'发送失败'}";
					}
				}
			}
			jsonStr = "{success:true,data:'成功发送"+successFlag+"个，失败"+errorFlag+"个'}";
		return SUCCESS;
		
	}

	

}
